﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using ZFSDomain.Common.CoreLib;
using ZFSDomain.Interface.Base;

namespace ZFSDomain.SysModule
{
    /// <summary>
    /// 主窗口功能操作基类
    /// </summary>
    public class BaseOperation : ViewModelBase, IDataOperation, IPermission
    {
        #region 属性

        private object currentRow;
        private string searchText = string.Empty;
        private ObservableCollection<ToolBarDefault> buttonDefaults;

        /// <summary>
        /// 搜索内容
        /// </summary>
        public string SearchText
        {
            get { return searchText; }
            set { searchText = value; RaisePropertyChanged(); }
        }

        /// <summary>
        /// 功能集合
        /// </summary>
        public ObservableCollection<ToolBarDefault> ButtonDefaults
        {
            get { return buttonDefaults; }
            set { buttonDefaults = value; RaisePropertyChanged(); }
        }

        /// <summary>
        /// 当前所选择行数据
        /// </summary>
        public object CurrentRow
        {
            get { return currentRow; }
            set { currentRow = value; RaisePropertyChanged(); }
        }

        #endregion

        #region 默认实现

        /// <summary>
        /// 初始化
        /// </summary>
        public virtual void InitViewModel()
        {
            this.SetDefaultButton();
            this.SetButtonAuth();
        }

        /// <summary>
        /// 设置默认按钮
        /// </summary>
        protected virtual void SetDefaultButton()
        {
            ButtonDefaults = new ObservableCollection<ToolBarDefault>();
            ButtonDefaults.Add(new ToolBarDefault() { AuthValue = Authority.ADD, ModuleName = "新增", Command = this.AddCommand });
            ButtonDefaults.Add(new ToolBarDefault() { AuthValue = Authority.EDIT, ModuleName = "编辑", Command = this.EditCommand });
            ButtonDefaults.Add(new ToolBarDefault() { AuthValue = Authority.DELETE, ModuleName = "删除", Command = this.DelCommand });
        }

        #endregion

        #region 功能命令

        private RelayCommand _addCommand;
        private RelayCommand _editCommand;
        private RelayCommand _delCommand;
        private RelayCommand _queryCommand;
        private RelayCommand _resetCommand;

        /// <summary>
        /// 新增
        /// </summary>
        public RelayCommand AddCommand
        {
            get
            {
                if (_addCommand == null)
                {
                    _addCommand = new RelayCommand(() => Add());
                }
                return _addCommand;
            }
            set { _addCommand = value; }
        }

        /// <summary>
        /// 编辑
        /// </summary>
        public RelayCommand EditCommand
        {
            get
            {
                if (_editCommand == null)
                {
                    _editCommand = new RelayCommand(() => Edit());
                }
                return _editCommand;
            }
            set { _editCommand = value; }
        }

        /// <summary>
        /// 删除
        /// </summary>
        public RelayCommand DelCommand
        {
            get
            {
                if (_delCommand == null)
                {
                    _delCommand = new RelayCommand(() => Del());
                }
                return _delCommand;
            }
            set { _delCommand = value; }
        }

        /// <summary>
        /// 查询
        /// </summary>
        public RelayCommand QueryCommand
        {
            get
            {
                if (_queryCommand == null)
                {
                    _queryCommand = new RelayCommand(() => Query());
                }
                return _queryCommand;
            }
            set { _queryCommand = value; }
        }

        /// <summary>
        /// 重置
        /// </summary>
        public RelayCommand ResetCommand
        {
            get
            {
                if (_resetCommand == null)
                {
                    _resetCommand = new RelayCommand(() => Reset());
                }
                return _resetCommand;
            }
            set { _resetCommand = value; }
        }



        #endregion

        #region IDataOperation接口

        /// <summary>
        /// 新增
        /// </summary>
        public virtual void Add()
        {
        }

        /// <summary>
        /// 编辑
        /// </summary>
        public virtual void Edit()
        {
        }

        /// <summary>
        /// 删除
        /// </summary>
        public virtual void Del()
        {
        }

        /// <summary>
        /// 查询
        /// </summary>
        public virtual void Query()
        {
        }

        /// <summary>
        /// 重置
        /// </summary>
        public virtual void Reset()
        {
            this.SearchText = string.Empty;
        }

        #endregion

        #region IPermission接口

        protected int? authValue;

        /// <summary>
        /// 权限值
        /// </summary>
        public int? AuthValue { get { return authValue; } set { authValue = value; } }

        /// <summary>
        /// 验证按钮权限
        /// </summary>
        /// <param name="authValue"></param>
        /// <returns></returns>
        public virtual bool GetButtonAuth(int authValue)
        {
            var def = ButtonDefaults.FirstOrDefault(t => (authValue & t.AuthValue) == t.AuthValue && t.IsVisibility.Equals(false));

            if (def != null)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 设置权限
        /// </summary>
        public virtual void SetButtonAuth()
        {
            if (Loginer.LoginerUser.IsAdmin) return;
            
            foreach (var b in ButtonDefaults)
                if ((this.AuthValue & b.AuthValue) != b.AuthValue)
                    b.IsVisibility = true; //隐藏功能
        }

        #endregion
    }

    /// <summary>
    /// 弹出式窗口基类
    /// </summary>
    public class BaseDialogOperation : ViewModelBase
    {
        /// <summary>
        /// 结果，用户点击确定Result=true;
        /// </summary>
        public bool Result { get; set; }

        private RelayCommand _saveCommand;
        private RelayCommand _cancelCommand;

        /// <summary>
        /// 保存
        /// </summary>
        public RelayCommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new RelayCommand(() => Save());
                }
                return _saveCommand;
            }
            set { _saveCommand = value; }
        }

        /// <summary>
        /// 取消
        /// </summary>
        public RelayCommand CancelCommand
        {
            get
            {
                if (_cancelCommand == null)
                {
                    _cancelCommand = new RelayCommand(() => Cancel());
                }
                return _cancelCommand;
            }
            set { _cancelCommand = value; }
        }

        public virtual void Save() { this.Result = true; Messenger.Default.Send("", "CloseWindow"); }
        public virtual void Cancel() { this.Result = false; Messenger.Default.Send("", "CloseWindow"); }

    }
}
